import { ref } from 'vue'

function genMockMeetings(type, count, creator = '我', join = false) {
  const statusMap = {
    notStarted: '未开始',
    ongoing: '进行中',
    ended: '已结束',
    cancel: '已取消',
  }
  const arr = []
  for (let i = 1; i <= count; i++) {
    arr.push({
      id: `${type}-${i}`,
      title: `${statusMap[type] || '会议'}主题${i}`,
      startTime: `2024-06-${(i % 28 + 1).toString().padStart(2, '0')} 0${i % 10}:00`,
      duration: 30 + (i % 5) * 15,
      status: statusMap[type],
      creator: creator,
      creatorName: creator,
      participants: Array.from({ length: 10 + (i % 8) }, (_, idx) => ({
        id: idx + 1,
        name: `用户${idx + 1}`,
        avatar: `https://api.dicebear.com/7.x/miniavs/svg?seed=${type}${i}${idx}`,
        role: idx === 0 ? '主持人' : (idx % 3 === 0 ? '嘉宾' : '参会者'),
        joinTime: `2024-06-${(i % 28 + 1).toString().padStart(2, '0')} 0${(idx + 1) % 10}:0${(idx + 2) % 6}`,
        status: idx % 4 === 0 ? '在线' : (idx % 4 === 1 ? '离线' : '忙碌'),
      })),
      detail: `这是${statusMap[type]}会议的详细描述${i}，包含议程、注意事项、联系人等丰富内容。`,
      cover: `https://picsum.photos/seed/meeting${type}${i}/60/60`,
    })
  }
  return arr
}

const allMeetings = ref([
  ...genMockMeetings('notStarted', 15),
  ...genMockMeetings('ongoing', 15),
  ...genMockMeetings('ended', 15),
  ...genMockMeetings('cancel', 15),
])
const myMeetings = ref(allMeetings.value.slice(0, 20))
const joinedMeetings = ref(allMeetings.value.slice(10, 25).map(m => ({ ...m, creator: '其他人' })))

export function useMeetingMock() {
  function addMeeting(meeting, type) {
    allMeetings.value.unshift({ ...meeting, id: `mine-${Date.now()}` })
    if (type === 'mine') myMeetings.value.unshift(meeting)
  }
  function updateMeeting(meeting, type) {
    const idx = allMeetings.value.findIndex(m => m.id === meeting.id)
    if (idx !== -1) allMeetings.value[idx] = { ...meeting }
    if (type === 'mine') {
      const idx2 = myMeetings.value.findIndex(m => m.id === meeting.id)
      if (idx2 !== -1) myMeetings.value[idx2] = { ...meeting }
    }
  }
  function deleteMeeting(id, type) {
    allMeetings.value = allMeetings.value.filter(m => m.id !== id)
    if (type === 'mine') myMeetings.value = myMeetings.value.filter(m => m.id !== id)
  }
  function exitMeeting(id) {
    joinedMeetings.value = joinedMeetings.value.filter(m => m.id !== id)
  }
  return {
    allMeetings,
    myMeetings,
    joinedMeetings,
    addMeeting,
    updateMeeting,
    deleteMeeting,
    exitMeeting
  }
} 